package com.loubobooo.personal.blog.controller.backend;

import com.loubobooo.personal.blog.entity.Options;
import com.loubobooo.personal.blog.entity.vo.OptionsVO;
import com.loubobooo.personal.blog.service.OptionsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

@Controller @RequestMapping("/admin/options") public class OptionsManageController {

    @Autowired private OptionsService optionsService;

    //基本信息显示
    @RequestMapping(value = "") public ModelAndView index() {
        ModelAndView modelAndView = new ModelAndView();
        OptionsVO optionsVO = optionsService.getOptions();
        modelAndView.addObject("optionsVO", optionsVO);

        modelAndView.setViewName("Admin/Options/index");
        return modelAndView;
    }

    //编辑基本信息显示
    @RequestMapping(value = "/edit") public ModelAndView editOptionView() {
        ModelAndView modelAndView = new ModelAndView();
        OptionsVO optionsVO = optionsService.getOptions();
        modelAndView.addObject("optionsVO", optionsVO);

        modelAndView.setViewName("Admin/Options/edit");
        return modelAndView;
    }

    //编辑基本信息提交
    @RequestMapping(value = "/editSubmit", method = RequestMethod.POST) public String editOptionSubmit(
            Options options) {
        //如果记录不存在，那就新建
        OptionsVO optionsVO = optionsService.getOptions();
        if (optionsVO.getOptionId() == null) {
            optionsService.insertOptions(options);
        }
        else {
            optionsService.updateOptions(options);
        }
        return "redirect:/admin/options";
    }

}
